package com.moeciyo.android.moeciyo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.moeciyo.android.moeciyo.R;

import java.util.ArrayList;

import butterknife.BindView;
import devlight.io.library.ntb.NavigationTabBar;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by geyifeng on 2017/8/12.
 */

public class MainFragment extends BaseFragment implements View.OnClickListener {

    private SupportFragment[] mFragments = new SupportFragment[5];
    @BindView(R.id.bottomBar)
    NavigationTabBar navigationTabBar;
    public static final int HOME = 0;
    public static final int PARTITION = 1;
    public static final int CIRCCLE = 2;
    public static final int MESSAGE = 3;
    public static final int USER = 4;


    public static MainFragment newInstance() {
        Bundle args = new Bundle();
        MainFragment fragment = new MainFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_main;
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SupportFragment homeFragment = findChildFragment(HomeFragment.class);
        if (homeFragment == null) {
            mFragments[HOME] = HomeFragment.newInstance();
            mFragments[PARTITION] = PartitionFragmen.newInstance();
            mFragments[CIRCCLE] = CircleFragment.newInstance();
            mFragments[MESSAGE] = MessageFragment.newInstance();
            mFragments[USER] = UserFragment.newInstance();

            loadMultipleRootFragment(R.id.content, HOME,
                    mFragments[HOME],
                    mFragments[PARTITION],
                    mFragments[CIRCCLE],
                    mFragments[MESSAGE],
                    mFragments[USER]);
        } else {
            // 这里库已经做了Fragment恢复,所有不需要额外的处理了, 不会出现重叠问题
            // 这里我们需要拿到mFragments的引用,也可以通过getChildFragmentManager.findFragmentByTag自行进行判断查找(效率更高些),用下面的方法查找更方便些
            mFragments[HOME] = homeFragment;
            mFragments[PARTITION] = findChildFragment(PartitionFragmen.class);
            mFragments[CIRCCLE] = findChildFragment(CircleFragment.class);
            mFragments[MESSAGE] = findChildFragment(MessageFragment.class);
            mFragments[USER] = findChildFragment(UserFragment.class);
        }
    }

    @Override
    protected void initView() {

        final ArrayList<NavigationTabBar.Model> models = new ArrayList<>();
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_home),
                        getResources().getColor(R.color.acbottombgcolor)
                ).title("首页")
                        .badgeTitle("1")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_home),
                        getResources().getColor(R.color.acbottombgcolor)
                ).title("分区")
                        .badgeTitle("1")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_home),
                        getResources().getColor(R.color.acbottombgcolor)
                ).title("圈子")
                        .badgeTitle("1")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_home),
                        getResources().getColor(R.color.acbottombgcolor)
                ).title("消息")
                        .badgeTitle("1")
                        .build()
        );
        models.add(
                new NavigationTabBar.Model.Builder(
                        getResources().getDrawable(R.drawable.ic_home),
                        getResources().getColor(R.color.acbottombgcolor)
                ).title("我的")
                        .badgeTitle("1")
                        .build()
        );
        navigationTabBar.setModels(models);
        navigationTabBar.setModelIndex(0,false);
        navigationTabBar.setOnTabBarSelectedIndexListener(new NavigationTabBar.OnTabBarSelectedIndexListener() {
            @Override
            public void onStartTabSelected(final NavigationTabBar.Model model, final int index) {
                showHideFragment(mFragments[index]);

            }

            @Override
            public void onEndTabSelected(final NavigationTabBar.Model model, final int index) {

            }
        });
    }

    @Override
    protected void setListener() {

    }

    @Override
    public void onClick(View v) {

    }
}